#include "bluetoothTask.h"
#include "usart3.h"
#include <stdio.h>
#include <string.h>
#include "FreertosCommon.h"
#include "BluetoothMsgParser.h"
extern QueueHandle_t configMsgQueueHandle;

int checkConfigMsg(int msgType);
void bluetoothTaskInit(){
	
	//初始化usart3
	usart3_init(9600);
	
}


void bluetoothTaskTaskFun(void * p){

		
	char data[200];
	while (1) {
			/*
			 //从HC - 05接收数据
			memset(data, 0, 200);
			int index = 0;
			while (1) {
					while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
				  char res = USART_ReceiveData(USART3);
					if(res=='\0') continue;  //一直读空的情况不要
					if(res=='\n'){//已经结束
							data[index] = '\0';
						  break;
					}else{
							data[index] = res;
					}
					index++;
			}

			printf("%s..............bluetooth.....\r\n",data);
			 */
		  char* msg = "{\"msgType\":1,\"content\":{\"state\":15,\"temp\":20,\"temp1\":20,\"humi\":20,\"humi1\":20,\"smoke\":20,\"sunshine\":20}}";
			int msgType = parseMsgType(msg);
				 // 需要判定是否是配置消息，是才发送到消息队列给配置模块使用 ？？？？ 怎么区别是否配置消息
			if(checkConfigMsg(msgType)){
				if (xQueueSend(configMsgQueueHandle, &msg, portMAX_DELAY) != pdPASS) {
						// 处理发送失败的情况
						printf("send data error\r\n");
				}
			}else{
			     
				  printf("other msg data handle\r\n");
			}
	
			
			vTaskDelay(2000);

	}

	
}

int checkConfigMsg(int msgType){
	//判定是否是配置消息
	return (msgType==MSG_TYPE_CONFIG_TH || msgType==MSG_TYPE_CONFIG_NETWORK) ? 1: 0 ;
}

void bluetoothTaskCreate(){

	//创建一个启动任务
		BaseType_t result = xTaskCreate(
			bluetoothTaskTaskFun,
			BLUETOOTH_TASK_NAME,
			BLUETOOTH_TASK_STACK_DEPTH,
			NULL,
			BLUETOOTH_TASK_PRIORITY,
			NULL
		);
	
	
	if(result==pdPASS)
		printf("create bluetoothTask task sucess!\r\n");
	else
	  printf("create bluetoothTask task fail!\r\n");
	
}
	